package com.zh.system.controller;

import com.zh.common.annotation.Operator;
import com.zh.system.domain.dto.*;
import com.zh.system.domain.entity.SysMsgFrom;
import com.zh.system.domain.entity.SysMsgTemplate;
import com.zh.system.domain.vo.MessageVo;
import com.zh.system.service.SysMessageService;
import com.zh.system.service.SysMessageTemplateService;
import com.zh.web.base.ApiResult;
import com.zh.web.base.BaseController;
import com.zh.web.base.PageApiResult;
import com.zh.web.domain.system.dto.MessageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Valid
@Slf4j
@RestController
@RequiredArgsConstructor
@Operator
@RequestMapping("/message")
@Api(tags = "系统服务-消息中心", value = "message center")
public class MessageController extends BaseController {

    private final SysMessageService sysMessageService;
    private final SysMessageTemplateService sysMessageTemplateService;

    @PostMapping
    @ApiOperation("发送消息")
    public ApiResult<String> sendMessage(@RequestBody MessageDto messageDto) {
        messageDto.setCreateOperator(operator);
        messageDto.setUpdateOperator(operator);
        return buildSuccessPackage(sysMessageService.sendMessage(messageDto));
    }

    @PutMapping
    @ApiOperation("确认消息")
    public ApiResult confirmMessage(@RequestBody ConfirmMessageDto confirmMessageDto) {
        confirmMessageDto.setUpdateOperator(operator);
        sysMessageService.confirmMessage(confirmMessageDto);
        return buildDefaultPackage();
    }
    @PutMapping("/type")
    @ApiOperation("确认type类型的消息")
    public ApiResult confirmMessageByType(@ApiParam("用户id") @RequestParam("id") Long id,@ApiParam("消息类型")  @RequestParam("type") String type) {
        sysMessageService.confirmMessageByType(id,type);
        return buildDefaultPackage();
    }
    @PostMapping("/page")
    @ApiOperation("按类型分页查询消息,请前端自行过滤系统消息")
    public ApiResult<PageApiResult<SysMsgFrom>> pageMessage(@RequestBody PageMessageDto pageMessageDto) {
        return buildSuccessPackage(sysMessageService.page(pageMessageDto));
    }

    @GetMapping
    @ApiOperation("用户-查询消息")
    public ApiResult<SysMsgFrom> getMessage(@ApiParam("消息id") @RequestParam("id") Long id) {
        return buildSuccessPackage(sysMessageService.getMessageById(id));
    }

    @GetMapping("/list")
    @ApiOperation("用户-查询消息列表 不传入type查询全部")
    public ApiResult<List<MessageVo>> listMessage(@ApiParam("用户id") @RequestParam("id") Long id, @RequestParam(value = "type", required = false) String type) {
        return buildSuccessPackage(sysMessageService.listMessage(id, type));
    }

    @GetMapping("/count")
    @ApiOperation("用户-查询未读消息数,不传入type查询全部")
    public ApiResult<Integer> countUnConfirmedMessage(@RequestParam("id") Long id, @RequestParam(value = "type", required = false) String type) {
        return buildSuccessPackage(sysMessageService.countUnConfirmedMessage(id, type));
    }

    @ApiOperation("新增消息模板")
    @PostMapping("/template")
    public ApiResult<String> addTemplate(@RequestBody AddMsgTemplateDto addMsgTemplateDto) {
        addMsgTemplateDto.setCreateOperator(operator);
        addMsgTemplateDto.setUpdateOperator(operator);
        return buildMsgPackage(sysMessageTemplateService.addTemplate(addMsgTemplateDto));
    }

    @GetMapping("/template")
    @ApiOperation("查询消息模板")
    public ApiResult<SysMsgTemplate> getTemplate(@RequestParam("id") @NotNull Long id) {
        return buildSuccessPackage(sysMessageTemplateService.getTemplateById(id));
    }

    @PutMapping("/template")
    @ApiOperation("修改模板状态")
    public ApiResult<String> updateTemplate(@RequestBody UpdateMessageTemplateDto updateMessageTemplateDto) {
        return buildMsgPackage(sysMessageTemplateService.updateTemplate(updateMessageTemplateDto));
    }

    @ApiOperation("删除模板")
    @DeleteMapping("/template")
    public ApiResult<String> deleteTemplate(@RequestParam("id") Long id) {
        return buildMsgPackage(sysMessageTemplateService.deleteTemplate(id));
    }

    @ApiOperation("分页查询模板")
    @PostMapping("/template/page")
    public ApiResult<PageApiResult<SysMsgTemplate>> pageTemplate(@RequestBody PageTemplateDto pageTemplateDto) {
        return buildSuccessPackage(sysMessageTemplateService.pageTemplate(pageTemplateDto));
    }
}
